import 'package:dio/dio.dart';
import 'package:myapp/common/store.dart';

import 'dio_api.dart';

const host = "https://lizo.top"; // api地址
MyAppApi myAppApi = MyAppApi();

class CustomInterceptors extends InterceptorsWrapper {
  @override
  Future onRequest(RequestOptions options) {
    options.headers["Authorization"] = dataStore.getAuthorizationToken();
    print("REQUEST[${options?.method}] => PATH: ${options?.path}");
    return super.onRequest(options);
  }

  // 2XX
  @override
  Future onResponse(Response response) {
    if (response.statusCode == 200) {
      if (response.data.code != "2000") {
        throw Future.error(response.data.message);
      } else {
        print("RESPONSE[${response?.statusCode}] => PATH: ${response?.request?.path}");
        return super.onResponse(response);
      }
    } else {
      throw Future.error(response.data.toString());
    }
  }

  // 300 400 500
  @override
  Future onError(DioError err) {
    print("ERROR[${err?.response?.statusCode}] => PATH: ${err?.request?.path}");
    return super.onError(err);
  }
}

class MyAppApi extends MyDio {
  MyAppApi() {
    dio.interceptors.add(CustomInterceptors());
  }

}
